﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisVenda.Data;
using SisVenda.Business;
using SisVenda.Web.Administrador.Fn;
using SisVenda.Web.Code;
using System.Data;
using SisVenda.Web.messageBox;

namespace SisVenda.Web.Administrador
{
    public partial class Register : System.Web.UI.Page
    {
        DropDownList ddlRegras;
        DropDownList ddlFuncionario;
        protected void Page_Load(object sender, EventArgs e)
        {
            MessageBox.Include(this);

            ddlRegras = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("ddlRegras");
            ddlFuncionario = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("ddlFuncionario");
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
            string[] funcoes = Roles.GetAllRoles();
            foreach (string item in funcoes)
            {
                ddlRegras.Items.Add(item);

            }

            ObjectDataSource odsFuncionario = (ObjectDataSource)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("odsFuncionario");
            odsFuncionario.DataBind();
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            //FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            ddlRegras = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("ddlRegras");
            ddlFuncionario = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("ddlFuncionario");

            Roles.AddUserToRole(RegisterUser.UserName, ddlRegras.SelectedValue);

            UsuarioFn userFn = new UsuarioFn();
            userFn.AddSenhaFuncionario(Convert.ToInt32(ddlFuncionario.SelectedValue), RegisterUser.UserName);

            //string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            //if (String.IsNullOrEmpty(continueUrl))
            //{
            //continueUrl = "~/AutenticacaoForm.aspx";
            //}
            //Response.Redirect(continueUrl);
        }

        protected void ContinueButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
    }

}
